package com.uetoken.cn.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.Spanned;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.uetoken.cn.R;
import com.uetoken.cn.bean.SubPursesBean;
import com.uetoken.cn.utils.Save2DecimalUtils;

import java.math.BigDecimal;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ChildAccountAdapter extends RecyclerView.Adapter<ChildAccountAdapter.MyViewHolder> {

    private Context mContext;
    private List<SubPursesBean.DataBean> childPurseList;
    private MyItemClickListener mListener;

    public ChildAccountAdapter(Context context) {
        this.mContext = context;
    }

    public ChildAccountAdapter(Context context, @Nullable List<SubPursesBean.DataBean> data) {
        this.mContext = context;
        this.childPurseList = data;
    }

    public void setData(List<SubPursesBean.DataBean> data) {
        this.childPurseList = data;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_child_account_view, parent, false);
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position) {
        SubPursesBean.DataBean mChildPurse = childPurseList.get(position);
        String mAccountName = mChildPurse.getPursename() + "\n(" + mChildPurse.getPursecode() + ")";
        Spanned mBalance = Html.fromHtml(mContext.getResources().getString(R.string.str_balance_1, Save2DecimalUtils.getDecimalPrecision(mChildPurse.getBalance().toString(),2)));
        Spanned mFreeze;
        if (mChildPurse.getFreeze().compareTo(BigDecimal.ZERO)> 0) {
            mFreeze = Html.fromHtml(mContext.getResources().getString(R.string.str_freeze_2, " " + Save2DecimalUtils.getDecimalPrecision(mChildPurse.getFreeze().toString(),2)));
        } else {
            mFreeze = Html.fromHtml(mContext.getResources().getString(R.string.str_freeze_1, " " +Save2DecimalUtils.getDecimalPrecision(mChildPurse.getFreeze().toString(),2)));
        }
        holder.mAccountNameTv.setText(mAccountName);
        holder.mBalanceTv.setText(mBalance);
        holder.mFreezeTv.setText(mFreeze);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onItemClick(v, position);
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return childPurseList == null ? 0 : childPurseList.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.accountNameTv)
        TextView mAccountNameTv;
        @BindView(R.id.balanceTv)
        TextView mBalanceTv;
        @BindView(R.id.freezeTv)
        TextView mFreezeTv;
        @BindView(R.id.arrowRightIv)
        ImageView mArrowRightIv;
        @BindView(R.id.childAccountLayout)
        RelativeLayout mChildAccountLayout;

        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public void setMyItemClickListener(MyItemClickListener myItemClickListener) {
        this.mListener = myItemClickListener;
    }

    public interface MyItemClickListener {
        public void onItemClick(View view, int postion);
    }
}
